package com.sina.gfund;

import com.sina.stock.SinaStockClient;
import com.sina.stock.SinaStockInfo;
import com.sina.oefund.SinaOEFund.ParseStockInfoException;
import com.sina.oefund.SinaOEFundClient;
import com.sina.oefund.SinaOEFund;

import java.util.List;
import java.util.ArrayList;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: qtang
 * Date: Jul 5, 2013
 * Time: 9:29:58 AM
 * To change this template use File | Settings | File Templates.
 */
public class GFundCalculator {

//    SinaStockClient sClient;
//    SinaOEFundClient fClient;

    //get static values
    List<GFundBean> gFundBeanList = new ArrayList<GFundBean>();
    int groupNum;

    public GFundCalculator(){
        gFundBeanList.add(new GFundBean("sz150018",50,"sz150019",50,"jj161812","sz399330",0.95));
        gFundBeanList.add(new GFundBean("sz150022",50,"sz150023",50,"jj163109","sz399001",0.95));
        groupNum = gFundBeanList.size();
    }
    
    public GFundCalculator(List<GFundBean> gFundBeanList){
    	this.gFundBeanList = gFundBeanList;
    }

    //input data
    String[] fundACodes;
    String[] fundBCodes;
    String[] fundMCodes;
    String[] fundMIndexCodes;
    List<Integer> fundAShareList =  new ArrayList<Integer>();
    List<Integer> fundBShareList =  new ArrayList<Integer>();
    List<Double> fundMIndexRatioList =  new ArrayList<Double>();

    public void initialAllInputData(){
        fundACodes = new String[groupNum];
        fundBCodes = new String[groupNum];
        fundMCodes = new String[groupNum];
        fundMIndexCodes = new String[groupNum];
        int i=0;
        for (GFundBean bean : gFundBeanList) {
            fundACodes[i] = bean.getFundACode();
            fundBCodes[i] = bean.getFundBCode();
            fundMCodes[i] = bean.getFundMCode();
            fundMIndexCodes[i] = bean.getFundMIndexCode();
            fundAShareList.add(bean.getFundAShare());
            fundBShareList.add(bean.getFundBShare());
            fundMIndexRatioList.add(bean.getFundMIndexRatio());
            i++;
        }
    }

    //data by SinaStockClient
    List<Double> fundAPriceList;
    List<Double> fundBPriceList;
    List<SinaStockInfo> fundMIndexList;
    public void getStockData(SinaStockClient sClient){
        try {
            fundAPriceList = sClient.getStockPricesList(fundACodes);
            fundBPriceList = sClient.getStockPricesList(fundBCodes);

            //get mother fund index value
            fundMIndexList = sClient.getStockInfoList(fundMIndexCodes);

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (SinaStockInfo.ParseStockInfoException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } 
    }
 
    //data by SinaOEFundClient
    List<SinaOEFund> fundMList;
    public void getFundData(SinaOEFundClient fClient){
    	try {
			fundMList = fClient.getFundList(fundMCodes);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseStockInfoException e) {
			e.printStackTrace();
		}
    }
    
    //calculate data
    List<Double> fundMIndexRiseList = new ArrayList<Double>();
    List<Double> fundABPriceList = new ArrayList<Double>();
    List<Double> fundMValueLastDayList = new ArrayList<Double>();
    List<Double> fundMValueEstimateList = new ArrayList<Double>();
    List<Double> discountList = new ArrayList<Double>();
    
    public void calculateResult(){
        for (SinaStockInfo sInfo : fundMIndexList) {
            float mYestodayPrice = sInfo.getYestodayPrice();
            float mNowPrice = sInfo.getNowPrice();
            float rise = (mNowPrice - mYestodayPrice) / mYestodayPrice;
            fundMIndexRiseList.add(new Double(rise));
        }

        for (int i = 0 ; i< groupNum ;i++) {
            SinaOEFund fundM =  fundMList.get(i);
            double value = fundM.getPrice();
            fundMValueLastDayList.add(value);

            double ratio =  fundMIndexRatioList.get(i);
            double rise = fundMIndexRiseList.get(i);
            double esValue =  value * (1+ rise * ratio);
            fundMValueEstimateList.add(esValue);

            double fundAPrice = fundAPriceList.get(i);
            int fundAShare = fundAShareList.get(i);
            double fundBPrice = fundBPriceList.get(i);
            int fundBShare = fundBShareList.get(i);

            double fundABPrice = (fundAPrice * fundAShare + fundBPrice * fundBShare)/(fundAShare + fundBShare) ;
            fundABPriceList.add(fundABPrice);

            double discount = (esValue - fundABPrice)/esValue;
            discountList.add(discount);

        }
    }

    public List<GFundBeanResult> getResultDataList(){
        List<GFundBeanResult> resultList = new ArrayList<GFundBeanResult>();
        for (int i = 0 ; i< groupNum ;i++) {
            resultList.add(new GFundBeanResult(fundAPriceList.get(i),fundBPriceList.get(i),
                            fundABPriceList.get(i),fundMIndexRiseList.get(i),
                            fundMValueLastDayList.get(i),fundMValueEstimateList.get(i),discountList.get(i)));
        }
        return resultList;
    }

    
    /*    public void printResult(){
    System.out.println("---------------------------printResult-----------------------------");
    System.out.println("A_Price\tB_Price\tMerge_Price\tValue_LastDay\tValue_Estimate\tDiscount");
    for (int i = 0 ; i< groupNum ;i++) {
        System.out.print(fundAPriceList.get(i)+"\t");
        System.out.print(fundBPriceList.get(i)+"\t");
        System.out.print(fundABPriceList.get(i)+"\t");
        System.out.print(fundMIndexRiseList.get(i)+"\t");

        System.out.print(fundMValueLastDayList.get(i)+" ");
        System.out.print(fundMValueEstimateList.get(i)+"\t");
        System.out.print(discountList.get(i));

        System.out.println();
    }

    System.out.println("---------------------------printResult-----------------------------");

}*/
    
    public static void main(String[] args) {
          GFundCalculator cal = new GFundCalculator();
          cal.initialAllInputData();
          cal.getStockData(SinaStockClient.getInstance());
          cal.getFundData(SinaOEFundClient.getInstance());
          cal.calculateResult();
          
          List<GFundBeanResult> rList = cal.getResultDataList();

          for (GFundBeanResult gFundBeanResult : rList) {
              System.out.println(gFundBeanResult.toString());
          }
    }


}
